﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using University.Workshop.Model;
using System.Data;

namespace University.Workshop.DataAccess
{
    public class LogOnUser : IDisposable
    {
        public const string CnnString = @"Data Source=.\SQLEXPRESS;Initial Catalog=DataBaseHotel;Integrated Security=True";
        private SqlConnection cnn;
        public bool ValidOperator(string userName, string password)
        {
            cnn = new SqlConnection(CnnString);
            bool valid;
            SqlCommand cmd = cnn.CreateCommand();
            // Cambiando Marieta Gonzales 
            cmd.CommandText = "SELECT [UserName] ,[Password] FROM [DataBaseHotel].[dbo].[Administrator] WHERE  CONVERT(varbinary, UserName) =  CONVERT(varbinary, @UserName)  and  CONVERT(varbinary, Password) =  CONVERT(varbinary,@Password);";
            cmd.Parameters.AddWithValue("@UserName", userName);
            cmd.Parameters.AddWithValue("@Password", password);
            // FIN Cambiando 
            try
            {
                cnn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                    valid = true;
                else
                    valid = false;
                return valid;
            }
            catch (Exception e)
            {
                throw new ArgumentException("Error at login", e);
            }
            finally
            {
                if (cnn.State == System.Data.ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
        }

        public bool ThereOperator()
        {
            cnn = new SqlConnection(CnnString);
            bool valid;
            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandText = "SELECT [UserName] ,[Password] FROM [DataBaseHotel].[dbo].[Administrator]";

            try
            {
                cnn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                    valid = true;
                else
                    valid = false;
                return valid;
            }
            catch (Exception e)
            {
                throw new ArgumentException("Error at searching users", e);
            }
            finally
            {
                if (cnn.State == System.Data.ConnectionState.Open)
                {
                    cnn.Close();
                }
            } 
        }
        #region Miembros de IDisposable

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (cnn != null)
                {
                    cnn.Dispose();
                    cnn = null;
                }
            }
        }

        #endregion
    }
}
